﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace CentrifugalSealCalc
{
    public partial class Form1 : Form
    {
        #region Background Tasks

        private void bwConfig_DoWork(object sender, DoWorkEventArgs e)
        {
            Program.ExtractResource("Expellers.csv", Program.FilePathExpellers);
            Program.ExtractResource("ImpellersClosed.csv", Program.FilePathImpellersClosed);
            Program.ExtractResource("ImpellersOpen.csv", Program.FilePathImpellersOpen);

            // Load Expellers 
            if (File.Exists(Program.FilePathExpellers))
            {
                Global.Expellers = CsvParser.ReadExpellers(Program.FilePathExpellers);
            }

            // Load Impellers Closed
            if (File.Exists(Program.FilePathImpellersClosed))
            {
                Global.ImpellersClosed = CsvParser.ReadImpellers(Program.FilePathImpellersClosed);
            }

            // Load Impellers Open
            if (File.Exists(Program.FilePathImpellersOpen))
            {
                Global.ImpellersOpen = CsvParser.ReadImpellers(Program.FilePathImpellersOpen);
            }
        }

        private void LoadConfig()
        {
            BackgroundWorker bwConfig = new BackgroundWorker();
            bwConfig.DoWork += new DoWorkEventHandler(bwConfig_DoWork);
            bwConfig.RunWorkerAsync();
        }

        #endregion Background Tasks

        private void btnCalc_Click(object sender, EventArgs e)
        {
            Calc();
        }

        private void Calc()
        {
            // populate global values based on user input
            double.TryParse(txtH0.Text, out Global.H0);
            double.TryParse(txtHbep.Text, out Global.Hbep);
            double.TryParse(txtQbep.Text, out Global.Qbep);
            double.TryParse(txtInletGPressure.Text, out Global.InletGPressure);    //Inlet Gauge pressure 16/03/2012
            double.TryParse(txtSpecificGravity.Text, out Global.SpecificGravity);
            //txtSuctionHead.Text = (Global.InletGPressure /(Outputs.SpecificGravity * 9.8605)).ToString("0.00");  // Malith

            // create impeller object

            Impeller impeller = ((EImpellerType)cboImpellerType.SelectedIndex) == EImpellerType.Closed ?
                Global.GetImpellerClosed(txtImpellerName.Text) : Global.GetImpellerOpen(txtImpellerName.Text);

            if (impeller != null)
            {
                txtImpellerT.Text = impeller.T.ToString();
                txtImpellerR1.Text = impeller.R1.ToString();
                txtImpellerR2.Text = impeller.R2.ToString();
            }
            else
            {
                impeller = new Impeller();
                double.TryParse(txtImpellerT.Text, out impeller.T);
                double.TryParse(txtImpellerR1.Text, out impeller.R1);
                double.TryParse(txtImpellerR2.Text, out impeller.R2);
            }

            impeller.SetImpellerType((EImpellerType)cboImpellerType.SelectedIndex);

            double.TryParse(txtImpellerC.Text, out impeller.C);
            double.TryParse(txtSpeedNs.Text, out impeller.Ns);
            double.TryParse(txtImpellerDs.Text, out impeller.Ds);
            double.TryParse(txtQ.Text, out impeller.Q);

            // read expeller values
            Expeller expeller = Global.GetExpeller(txtExpellerName.Text);
            if (expeller != null)
            {
                txtExpellerT.Text = expeller.T.ToString();
                txtExpellerR1.Text = expeller.R1.ToString();
                txtExpellerR2.Text = expeller.R2.ToString();
            }
            else
            {
                expeller = new Expeller();
                double.TryParse(txtExpellerT.Text, out expeller.T);
                double.TryParse(txtExpellerR1.Text, out expeller.R1);
                double.TryParse(txtExpellerR2.Text, out expeller.R2);
            }

            double.TryParse(txtExpellerC.Text, out expeller.C);
            double.TryParse(txtSpeedNs.Text, out expeller.Ns);

            Calc calc = new Calc(impeller, expeller);
            double.TryParse(txtSpecificGravity.Text, out calc.SpecificGravity);
            Outputs outputs = calc.Start();

            txtAdvice.Text = outputs.Message;
            if (outputs.Safe)
                txtAdvice.BackColor = Color.LightGreen;
            else
                txtAdvice.BackColor = Color.Red;

            txtPmax1.Text = txtPmax.Text = (outputs.Pmax / 1000).ToString("0.00");     // note outputs O is in lower case but in clacs it is in upper case as in Method
            txtPinlet1.Text = txtPinlet.Text = (calc.Pinlet / 1000).ToString("0.00");
            txtSpeedNd.Text = outputs.Nd.ToString("0.00");
            txtImpellerR2new.Text = (outputs.dr * 1000).ToString("0.00");
            txtimpdr.Text = outputs.percReduction.ToString("0.00");
            txtPc.Text = (outputs.Pc / 1000).ToString("0.00");
            txtPdImp.Text = (outputs.PdropImp / 1000).ToString("0.00");
            txtPdExp.Text = (outputs.PdropExp / 1000).ToString("0.00");
            txtPhub.Text = (outputs.Phub / 1000).ToString("0.00");
            txtPgland.Text = (outputs.Pgland / 1000).ToString("0.00");
            txtSuctionHead.Text = outputs.SuctionHead.ToString("0.00");

            txtImpellerDs.Text = impeller.Ds.ToString("0.00");
            txtMessageImpellerType.Text = outputs.MessageImepellerCode;
            gpImpellerPropertiesNew.Visible = !outputs.Safe;

            pgCalc.SelectedObject = calc.Outputs;

           // ReportWriter writer = new ReportWriter(calc);
           // writer.WritePDF();
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cboImpellerType.Items.AddRange(Global.GetEnumDescriptions<EImpellerType>());
            cboImpellerType.SelectedIndex = (int)EImpellerType.Closed;

            UpdateLabels();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            LoadConfig();
        }

        private void txtSpeed_TextChanged(object sender, EventArgs e)
        {
            UpdateLabels();
        }

        private void UpdateLabels()
        {
            //     ttApp.SetToolTip(lblSuctionHead, "Maximum inlet gauge pressure from expeller curve at " + txtSpeedNs.Text + " rpm.");
        }

        private void txtImpellerName_TextChanged(object sender, EventArgs e)
        {
            if (txtImpellerName.Text.Length > 0)
                ReadOnlyImpellerProperties(true);
            else
                ReadOnlyImpellerProperties(false);
        }

        private void ReadOnlyImpellerProperties(bool readOnly)
        {
            txtImpellerT.Enabled = !readOnly;
            txtImpellerR1.Enabled = !readOnly;
            txtImpellerR2.Enabled = !readOnly;
        }

        private void ReadOnlyExpellerProperties(bool readOnly)
        {
            txtExpellerT.Enabled = !readOnly;
            txtExpellerR1.Enabled = !readOnly;
            txtExpellerR2.Enabled = !readOnly;
        }

        private void txtExpellerName_TextChanged(object sender, EventArgs e)
        {
            if (txtExpellerName.Text.Length > 0)
                ReadOnlyExpellerProperties(true);
            else
                ReadOnlyExpellerProperties(false);
        }

        private void pbDiagram_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

    }
}